import api from "@/api/customer"
import { Message } from 'iview'

export default {
    state: { 
        customerList: {
            list: [],
            page_size: 20,
            total: 0
        }, 
    },
    getters: { 
        customerList: state => state.customerList, 
    },
    actions: { 
        fetchCustomerList({commit}, condition){ 
            api.fetchCustomerList(condition).then(reply => { 
                if(reply.data.ok){
                    commit('FETCH_CUSTOMER_LIST', reply.data);  
                }else{
                    Message.error(reply.data.msg || "请求失败")
                }
            })
        },
        createCustomer({commit}, condition){ 
            api.createCustomer(condition.params).then(reply => {   
                condition.cb && condition.cb(reply.data)
            })
        }, 
        modifyCustomer({commit}, condition){ 
            api.modifyCustomer(condition.params).then(reply => {   
                condition.cb && condition.cb(reply.data)
            })
        }, 
        deleteCustomer({commit}, condition){ 
            api.deleteCustomer(condition.params).then(reply => {  
                condition.cb && condition.cb(reply.data)
            })
        },
        
    },
    mutations: {
        FETCH_CUSTOMER_LIST(state, data){
            state.customerList.list = data.data 
            state.customerList.page_size = data.paginator.PageSize 
            state.customerList.total = data.paginator.total 
        }, 
    }
}